library(foreign)
library(margins)
library(multiwayvcov)
library(car)
library(effects)
library(lmtest)
library(stargazer)
library(Zelig)
library(pscl)
setwd('/Users/joelsievert/Dropbox/Research/Electoral Institutions/DRDR/journal submissions/JPIPE/final submission/Replication Materials')


d <- read.csv('jpipe_appendix.csv')

##############
##APPENDIX B##
##############

###########
#TABLE B-1#
###########


d1 <- subset(d, current_statewide == 1)


t1 <- xtabs(~future_sen + statewide, d1)
prop.table(t1, 2)
chisq.test(t1)

#m1 <- glm(future_sen ~ statewide + as.factor(state_icpsr), family = "binomial", d)
#o1 <- coeftest(m1, vcov = cluster.vcov(m1, d$state_icpsr))

m1 <- glm(future_sen ~ statewide +  as.factor(state_icpsr), family = "binomial", d1)
o1 <- coeftest(m1, vcov = cluster.vcov(m1, d1$state_icpsr))

hitmiss(m1)


m2 <- glm(future_sen ~ statewide +pol_dynasty + as.factor(gov_lvl) + as.factor(state_icpsr), family = "binomial", d1)
o2 <- coeftest(m2, vcov = cluster.vcov(m2, d1$state_icpsr))

stargazer(o1, o2, digits = 2)


#############
##TABLE B-2##
#############

d$dem <- NA
d$dem[d$party == 100] <- 1
d$dem[d$party == 200] <- 0

d1 <- subset(d, dem == 1)
d2 <- subset(d, dem == 0)

d1b <- subset(d, dem == 1 & current_statewide == 1)
d2b <- subset(d, dem == 0 & current_statewide == 1)

######################
##LEGISLATIVE VOTING##
######################

#common space scores

m1 <- lm(common1 ~ statewide + as.factor(state_icpsr), d1b)
o1 <- coeftest(m1, vcov = cluster.vcov(m1, d1b$state_icpsr))

m2 <- lm(common1 ~ statewide + as.factor(state_icpsr), d2b)
o2 <- coeftest(m2, vcov = cluster.vcov(m2, d2b$state_icpsr))


#party unity

m3 <- lm(unity ~ statewide + as.factor(state_icpsr)+ as.factor(congress), d1b)
o3 <- coeftest(m3, vcov = cluster.vcov(m3, d1b$state_icpsr))

m4 <- lm(unity ~ statewide + as.factor(state_icpsr)+ as.factor(congress), d2b)
o4 <- coeftest(m4, vcov = cluster.vcov(m4, d2b$state_icpsr))

#standardized unity scores
m5 <- lm(h_dem_std ~ statewide + as.factor(state_icpsr), d1b)
o5 <- coeftest(m5, vcov = cluster.vcov(m5, d1b$state_icpsr))

m6 <- lm(h_dem_std ~ statewide + as.factor(state_icpsr), d2b)
o6 <- coeftest(m6, vcov = cluster.vcov(m6, d2b$state_icpsr))


stargazer(o1, o2, o3, o4, o5, o6, digits = 2)


